package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult = roleService.findPage(queryPageBean);
       return pageResult;
    }

    /**
     * 添加角色数据
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.add(role,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }
        return new Result(true, "添加角色成功");
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public Result del(Integer id){
        System.out.println(id);
        try {
            roleService.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
        return new Result(true, "删除角色成功");
    }

    /**
     * 根据Id查找
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
           Role role = roleService.findById(id);
            return new Result(true, "删除角色成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
    }

    /**
     * 通过角色Id查询权限Id
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,"查询权限Id成功",permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限Id失败");
        }
    }

    /**
     * 修改角色信息
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.edit(role,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑角色失败");
        }
        return new Result(true,"编辑角色失败");
    }

    /**
     * 查询全部角色
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roles = roleService.findAll();
            return new Result(true,"查询全部角色成功",roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询全部角色失败");
        }
    }

}
